#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# @Time    : 2021/2/24 下午8:20
# @Author  : HuiDong.Bai
# @Email   : huidong.bai@ximalaya.com
# @File    : download_log.py
# @Software: PyCharm
import os
import sys

import requests
from lxml import etree
import argparse
from concurrent.futures import ThreadPoolExecutor, as_completed

root_dir = os.path.dirname(__file__)
# 在当前路径下创建保存日志文件夹
log_path = os.path.join(root_dir, 'download_logs', 'logs')
if not os.path.exists(log_path):
    os.makedirs(log_path)


parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('-sn', type=str, default='XMKD011217600113')
parser.add_argument('-t', type=str, default='20210908')
parser.add_argument('-k', type=str, default='XMKD')
args = parser.parse_args()
sn = args.sn
date = args.t
ctype = args.k

# 下载列表
log_list = []


# 获取响应体token和seesion
def get_token_session(rsp):
    msg = rsp.headers.get('Set-Cookie')
    x_token = msg.split('; expires=')[0].split('=')[1]
    l_session = msg.split('; expires=')[1].split('laravel_session=')[1]
    return x_token, l_session


# 获取_token值
def get_token(rsp):
    html = etree.HTML(rsp.text)
    text = html.xpath("//*[@name='_token']/@value")
    return text[0]


def get_login_info():
    # login_url = 'https://admin-skill-pub.xiaoyastar.work/admin/auth/login'
    login_url = 'https://admin-skill-pub.xiaoyastar.com/admin/auth/login'
    headers = {'Host': 'admin-skill-pub.xiaoyastar.com'}
    resp = requests.get(url=login_url, headers=headers)

    temporary_token, temporary_session = get_token_session(resp)
    _token = get_token(resp)

    cookie = 'XSRF-TOKEN=%s;laravel_session=%s' % (temporary_token, temporary_session)
    re_headers = {'Host': 'admin-skill-pub.xiaoyastar.com', 'Cookie': cookie}

    # 用户名：baihuidong, 密码：123456
    data = {'username': 'baihuidong', 'password': '123456', '_token': _token}

    re_resp = requests.post(url=login_url, headers=re_headers, data=data)
    xsrf_token, laravel_session = get_token_session(re_resp)
    return xsrf_token, laravel_session


def get_log_list(next_url, header):
    if len(next_url):
        new_url = next_url[0]
        rep = requests.get(url=new_url, headers=header)
        html = etree.HTML(rep.text)
        log_links = html.xpath("//tr/td/a/@href")
        if len(log_links):
            global log_list
            log_list.extend(log_links)
        next2_url = html.xpath("//*[@id='example2_next']/a/@href")
        return get_log_list(next2_url, header)
    else:
        return 0


def download(path, link, headers):
    file_name = os.path.join(path, (sn + link.split(sn)[1]).replace('/', '-'))
    print('Downloading the %s' % file_name)
    down_file = requests.get(url=link, headers=headers)

    with open(file_name, "wb") as f:
        f.write(down_file.content)


def download_logs(headers):
    print('已找到共%s个日志文件，开始下载！' % len(log_list))
    # 获取日志重定向位置，在桌面下创建logs文件夹，日志将存在该文件夹下
    # desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
    # log_path = desktop_path + '/download_logs'
    # with ThreadPoolExecutor(max_workers=len(log_list)) as t:
    #     obj_list = []
    #     for link in log_list:
    #         obj = t.submit(download, log_path, link)
    #         obj_list.append(obj)
    for link in log_list:
        download(log_path, link, headers)
    print('下载完成！')


def merge_logs():
    file_list = []
    for file in os.listdir(log_path):
        if any(key in file for key in ['log', 'txt']):
            file_list.append(file)
    file_list = sorted(file_list)

    print('正在合并%s个日志文件！' % len(file_list))

    all_logs_file = open(os.path.join(root_dir, 'download_logs', 'all.log'), 'a+', encoding='UTF-8')

    for log_file in file_list:
        file = open(os.path.join(log_path, log_file), 'r', encoding='UTF-8')
        for buffer in file:
            all_logs_file.write(buffer)
        file.close()
    all_logs_file.close()
    print('合并完成！')


def main():
    xsrf_token, laravel_session = get_login_info()
    productions = {'XMKD': 'S_PROD8_1477',
                   'NANO': 'N_PROD1_NANO_TD',
                   'ROTS': 'S_PROD1_1849',
                   'MINI': 'S_PROD1_MEIDIMINI'}

    url = 'https://admin-skill-pub.xiaoyastar.com/admin/qCloud/selfLogList?newDir=%s&sn=%s&date=%s' % (productions.get(ctype), sn, date)
    cookie = 'XSRF-TOKEN=%s;laravel_session=%s' % (xsrf_token, laravel_session)
    headers = {'Host': 'admin-skill-pub.xiaoyastar.com', 'Cookie': cookie}

    re = requests.get(url=url, headers=headers)
    html = etree.HTML(re.text)
    log_links = html.xpath("//tr/td/a/@href")
    example2_next = html.xpath("//*[@id='example2_next']/a/@href")

    log_nums = len(log_links)
    if log_nums == 0:
        print('没找到任何相关日志，请检查!')
        sys.exit()

    # 保存日志连接
    global log_list
    log_list.extend(log_links)
    get_log_list(example2_next, headers)
    log_list = list(set(log_list))
    # 下载日志
    download_logs(headers)
    # 合并日志
    merge_logs()


if __name__ == '__main__':
    main()
